$(document).ready(function() {
    var compare = function (obj1, obj2) {
        var val1 = obj1.ope_id;
        var val2 = obj2.ope_id;
        if (val1 < val2) {
            return -1;
        } else if (val1 > val2) {
            return 1;
        } else {
            return 0;
        }
    };
    var VAL ={
        NORMAL : "0000000"  ,
        EVT_USER : $("#userId").text(),
        T_FBPBISOPE : 'FBPBISOPE',
        T_FBPBISTOL : 'FBPBISTOL',
        T_FDPAPLYWO : "FDPAPLYWO",
        T_MSPLINEWO : "MSPLINEWO",
        DISABLED_ATTR : {
            'disabled' : true
        },
        ENABLED_ATTR : {
            'disabled' : false
        }
    };

    var domObj = {
        $bayIdSel : $("#bayIdSel"),
        $opeIdSel : $("#opeIdSel"),
        $woIdSel  : $("#woIdSel"),
        buttons : {
            $query_btn  : $("#query_btn"),
            $save_btn   : $("#save_btn"),
            $del_btn    : $("#del_btn"),
            $active_btn : $("#active_btn"),
        },
        grids    : {
            $woListDiv  : $("#woListDiv"),
            $woListGrd  : $("#woListGrd"),
            $woListPg   : $("#woListPg")
        }
    };
    var iniGridInfo = function(){
        var woListCM = [
            {name: 'rpt_timestamp',    index: 'rpt_timestamp',         label:RPT_TIMESTAMP,          width: 240},
            {name: 'line_id',          index: 'line_id',            label:LINE_ID_TAG,            width: 240},
            {name: 'ope_id',           index: 'ope_id',             label:OPE_ID_TAG,             width: 240},
            {name: 'wo_id',            index: 'wo_id',              label:WO_ID_TAG,              width: 240},
            {name: 'valid_flg',        index: 'valid_flg',             label:ACTIVE_FLG_TAG,         width: 240},
        ];
        domObj.grids.$woListGrd.jqGrid({
            url: "",
            datatype: "local",
            mtype: "POST",
            autowidth: true,
            shrinkToFit:false,
            scroll: true,
            resizable: true,
            loadonce: true,
            viewrecords: true,
            rownumbers: true,
            emptyrecords: true,
            colModel: woListCM,
            pager: "#woListPg"
        });

    };
    var controlsFunc={
		resizeFnc : function(){
	        var offsetBottom, divWidth;
	        divWidth = $("#woListDiv").width();
	        offsetBottom = $(window).height() - $("#woListDiv").offset().top;
	        $("#woListDiv").height(offsetBottom * 0.95);
	        $("#woListGrd").setGridWidth(divWidth * 0.99);
	        $("#woListGrd").setGridHeight(offsetBottom * 0.99 - 100);
	    },
        iniBayInfo : function(){//初始化线别
            comAddValueByDataCateFnc("#bayIdSel", "AREA", "data_ext", "data_desc", true);
        },
        iniOpeInfo : function(){//初始化站点
            var inObj = {
                trx_id : VAL.T_FBPBISOPE,
                action_flg : "L"
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                var oaryA = outObj.oary;
                oaryA.sort(compare);
                SelectDom.addSelectArr_(domObj.$opeIdSel, oaryA, "ope_id","ope_id","ope_dsc",true);
            }
        },
        iniWoInfo : function(){//初始化工单
            var inObj = {
                trx_id: VAL.T_FDPAPLYWO,
                action_flg: "Q",
                iary: []
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                SelectDom.addSelectArr(domObj.$woIdSel, outObj.oary, "wo_id", "wo_id", "", true);
            }
        }
    };

    var btnFunc = {
        queryFunc  : function(){
            var bayIdFk = $.trim(domObj.$bayIdSel.val());
            var opeIdFk = ($.trim(domObj.$opeIdSel.val())).split("@");
            var opeId = opeIdFk[0];
            var woIdFk  = $.trim(domObj.$woIdSel.val());
            var inObj = {
                trx_id : VAL.T_MSPLINEWO,
                action_flg : "Q",
                line_id : bayIdFk,
                ope_id : opeId,
                wo_id : woIdFk
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                setGridInfo(outObj.oary, "#woListGrd", true);
            }
        },
        saveFunc   : function(){
            var bayIdFk = $.trim(domObj.$bayIdSel.val());
            if (!bayIdFk) {
                showErrorDialog("","请选择线别！");
                return false;
            }
            var opeIdFk = ($.trim(domObj.$opeIdSel.val())).split("@");
            var opeId = opeIdFk[0];
            if (!opeId) {
                showErrorDialog("","请选择站点代码！");
                return false;
            }
            var woIdFk  = $.trim(domObj.$woIdSel.val());
            if (!woIdFk) {
                showErrorDialog("","请选择工单代码！");
                return false;
            }
            var inObj = {
                trx_id : VAL.T_MSPLINEWO,
                action_flg : "A",
                line_id : bayIdFk,
                ope_id : opeId,
                wo_id : woIdFk,
                evt_usr : VAL.EVT_USER
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                showSuccessDialog("信息设定成功");
            }
            btnFunc.queryFunc();
        },
        delFunc    : function(){
            var rowId  = domObj.grids.$woListGrd.jqGrid('getGridParam','selrow');
            if(!rowId){
                showErrorDialog("","请至少选择一条设定信息进行删除!");
                return false;
            }
            var rowData = domObj.grids.$woListGrd.jqGrid("getRowData", rowId);
            var inObj = {
                trx_id : VAL.T_MSPLINEWO,
                action_flg : "D",
                line_id : rowData.line_id,
                ope_id : rowData.ope_id,
                wo_id : rowData.wo_id,
                evt_usr : VAL.EVT_USER
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                showSuccessDialog("删除成功");
            }
            btnFunc.queryFunc();
        },
        activeFunc : function(){
            var rowId = domObj.grids.$woListGrd.jqGrid('getGridParam','selrow');
            if(!rowId){
                showErrorDialog("","请至少选择一条设定信息进行激活/关闭!");
                return false;
            }
            var rowData = domObj.grids.$woListGrd.jqGrid("getRowData", rowId);
            var inObj = {
                trx_id : VAL.T_MSPLINEWO,
                action_flg : "S",
                line_id : rowData.line_id,
                ope_id : rowData.ope_id,
                wo_id : rowData.wo_id,
                evt_usr : VAL.EVT_USER
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code == VAL.NORMAL) {
                if(rowData.valid_flg=='Y'){
                    showSuccessDialog("关闭成功");
                }else{
                    showSuccessDialog("激活成功");
                }
            }
            btnFunc.queryFunc();
        },
    };

    /**
     * Ini contorl's data
     */
    var iniContorlData = function(){
        iniGridInfo();
        controlsFunc.resizeFnc();
        controlsFunc.iniBayInfo();
        controlsFunc.iniOpeInfo();
        controlsFunc.iniWoInfo();
    };

    /**
     * Bind button click action
     */
    var iniButtonAction = function(){
        domObj.buttons.$query_btn.click(function(){
            btnFunc.queryFunc();
        });
        domObj.buttons.$save_btn.click(function(){
            btnFunc.saveFunc();
        });
        domObj.buttons.$del_btn.click(function(){
            btnFunc.delFunc();
        });
        domObj.buttons.$active_btn.click(function(){
            btnFunc.activeFunc();
        });
    };

    /**
     * Other action bind
     */
    var otherActionBind = function(){
        $("form").submit(function(){
            return false;
        });

        $(window).resize(function() {
        	controlsFunc.resizeFnc();
        });
    };

    /**
     * Ini view, data and action bind
     */
    var initializationFunc = function(){
        iniContorlData();
        iniButtonAction();
        otherActionBind();
    };

    initializationFunc();
    //enter
   /* document.onkeydown=function(event){
        var e = event || window.event || arguments.callee.caller.arguments[0];
        if(e && e.keyCode==13){ // 按 enter
            $('.bootbox').modal("hide");
        }
    }*/
});